﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.SharePoint.WebPartPages;

namespace VH.SharePoint.Api.Entensions
{
    public static class WebPartExtentions
    {
        public static void SetContent(this ContentEditorWebPart wp, String content)
        {
            var contentInfo = wp.GetType().GetField("_content", BindingFlags.NonPublic | BindingFlags.Instance);
            if (contentInfo == null) throw new FieldAccessException("_content field is not available in the Content Editor Web Part");
            contentInfo.SetValue(wp, content);
        }

        public static String GetContent(this ContentEditorWebPart wp)
        {
            var contentInfo = wp.GetType().GetField("_content", BindingFlags.NonPublic | BindingFlags.Instance);
            if (contentInfo == null) throw new FieldAccessException("_content field is not available in the Content Editor Web Part");
            var value = contentInfo.GetValue(wp);
            return (String) value;
        }
    }
}
